package com.ctakit.kiwi.common.core.bean.selectors;

import com.ctakit.kiwi.common.core.bean.Selector;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Selector that implemented the select(List&lt;?&gt; from) methods.
 *
 * @author lizhenmin
 */
@Slf4j
public abstract class DefaultSelector<T> implements Selector<T> {

    @Override
    public List<T> select(List<?> from) {
        if (CollectionUtils.isEmpty(from)) {
            log.info("The from collection is empty, returning empty List");
            return Collections.emptyList();
        }

        return doSelect(from);
    }

    protected List<T> doSelect(List<?> notEmptyFrom) {
        List<T> result = new ArrayList<T>(notEmptyFrom.size());
        for (Object item : notEmptyFrom) {
            result.add(select(item));
        }
        return result;
    }

}
